package com.music.controller;

import com.music.pojo.Artist;
import com.music.pojo.Music;
import com.music.service.MusicService;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Controller;
import com.laoxuai.ibatis.util.PageInfo;
import com.laoxuai.web.mvc.annotation.RequestMapping;
import com.laoxuai.web.mvc.annotation.ResponseBody;
import com.music.util.Result;

import java.util.List;

@Controller
@RequestMapping("/music")
public class MusicController {

    @Autowired
    private MusicService musicServiceImpl;

    /***
     * 新增
     * @param music
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Result insert(Music music) {
        int result = musicServiceImpl.insert(music);
        if(result==-1){
            return Result.failed("歌曲名不能为NULL");
        }
        return Result.success("添加成功", result);
    }

    /***
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Result delete(Integer id) {
        int result = musicServiceImpl.delete(id);
        if(result==-1){
            return Result.failed("删除失败");
        }else {
            return Result.success("删除成功", result);
        }
    }

    /***
     * 修改
     * @param music
     * @param id
     * @return
     */
    @RequestMapping(value="/update")
    @ResponseBody
    public Result update(Integer id, Music music) {
        music.setId(id);
        int result = musicServiceImpl.update(music);
        if(result==-1){
            return Result.failed("歌曲已存在");
        }else{
            return Result.success("修改成功", result);
        }
    }

    /***
     * 主键查询
     * @param id
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public Result getByPrimaryKey(Integer id) {
        Music music = musicServiceImpl.getByPrimaryKey(id);
        return Result.success("查询成功", music);
    }

    @RequestMapping(value = "/page")
    @ResponseBody
    public Result page(Music music, int page, int size) {
        PageInfo<Music> pageInfo = musicServiceImpl.listPageInfo(music, page, size);
        return Result.success("查询成功", pageInfo);
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Result list(Music music, int page, int size) {
        List<Music> list = musicServiceImpl.listPage(music, page, size);
        return Result.success("查询成功", list);
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/selectAll")
    @ResponseBody
    public Result selectAll(){
        List<Music> list = musicServiceImpl.selectAll();
        if (list == null){
            return Result.failed("查询失败");
        }
        return Result.success("查询成功",list);
    }
}